﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace LabyCLI.Commands
{
    class cmd_shutdown
    {

        Terminal frm = Terminal.Instance;

        public void Main()
        {
            NativeMethods.Shutdown();
        }

        static class NativeMethods
        {
            public static void Shutdown()
            {
                ManagementBaseObject outParameters = null;
                ManagementClass sysOS = new ManagementClass("Win32_OperatingSystem");
                sysOS.Get();

                // You can't shutdown without security privileges
                sysOS.Scope.Options.EnablePrivileges = true;

                // Get our in parameters
                ManagementBaseObject inParameters = sysOS.GetMethodParameters("Win32Shutdown");

                // Flag 1 means we want to shut down the system. Use "2" to reboot.
                inParameters["Flags"] = "1";
                inParameters["Reserved"] = "0";
                foreach (ManagementObject manObj in sysOS.GetInstances())
                {
                    outParameters = manObj.InvokeMethod("Win32Shutdown", inParameters, null);
                }
            }
        }
    }
}
